package org.mte.sak.mail;

import java.util.ArrayList;

import org.mte.sak.file.FileObject;


/**
 * MailMessage.
 *
 * @author Marco Traversari
 * @version $Revision: 1.1 $ $Date: 2011-04-18 08:38:02 $
 */
public class MailMessage {

	/**
	 * Separatore dei destinatari
	 */
	public final static String ADDRESS_SEPARATOR = ",";

	/**
	 * from
	 */
	protected String from = "";

	/**
	 * to
	 */
	protected String to = "";

	/**
	 * cc
	 */
	protected String cc = "";

	/**
	 * bcc
	 */
	protected String bcc = "";

	/**
	 * subject
	 */
	protected String subject = null;

	/**
	 * body
	 */
	protected String body = null;

	/**
	 * attachments
	 */
	protected ArrayList<FileObject> attachments = null;

	/**
	 * MailMessage
	 */
	public MailMessage() {
	}

	/**
	 * MailMessage
	 * @param from
	 * @param to
	 * @param subject
	 * @param body
	 */
	public MailMessage(String from, String to,
				String subject, String body) {
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.body = body;
	}

	/**
	 * MailMessage
	 * @param from
	 * @param to
	 * @param cc
	 * @param bcc
	 * @param subject
	 * @param body
	 */
	public MailMessage(String from, String to,
			String cc, String bcc,
			String subject, String body) {
		this.from = from;
		this.to = to;
		this.cc = cc;
		this.bcc = bcc;
		this.subject = subject;
		this.body = body;
	}

	/**
	 * Restituisce la lista dei destinatari.
	 * @return stringa dei destinatari
	 */
	public String getTo() {
		if (to != null) {
			int lenTo = to.length();
			if (lenTo > 0) {
				String ch = to.substring(lenTo - 1);
				if (ch.equals(ADDRESS_SEPARATOR)) {
					to = to.substring(0, lenTo - 1);
				}
			}
		}
		return to;
	}

	/**
	 * Aggiunge destinatario.
	 *
	 * @param newTo
	 */
	public void addTo(String newTo) {
		if (newTo != null && to.indexOf(newTo) == -1) {
			to += newTo + ADDRESS_SEPARATOR;
		}
	}

	/**
	 * getAttachments
	 * @return attachments.
	 */
	public ArrayList<FileObject>getAttachments() {
		return attachments;
	}

	/**
	 * setAttachments
	 * @param attachments
	 */
	public void setAttachments(ArrayList<FileObject> attachments) {
		this.attachments = attachments;
	}

	/**
	 * getBcc
	 * @return bcc
	 */
	public String getBcc() {
		return bcc;
	}

	/**
	 * setBcc
	 * @param bcc
	 */
	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	/**
	 * getBody
	 * @return body
	 */
	public String getBody() {
		return body;
	}

	/**
	 * setBody
	 * @param body
	 */
	public void setBody(String body) {
		this.body = body;
	}

	/**
	 * getCc
	 * @return cc
	 */
	public String getCc() {
		return cc;
	}

	/**
	 * setCc
	 * @param cc
	 */
	public void setCc(String cc) {
		this.cc = cc;
	}

	/**
	 * getFrom
	 * @return from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * setFrom
	 * @param from
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * getSubject
	 * @return subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * setSubject
	 * @param subject
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * setTo
	 * @param to
	 */
	public void setTo(String to) {
		this.to = to;
	}

	/**
	 * Indica se il messaggio ha allegati
	 * @return true se c'� almeno un allegato.
	 */
	public boolean hasAttachment() {
		return (attachments != null && attachments.size() > 0);
	}

	/**
	 * getAttachment
	 * @param index
	 * @return un oggetto <code>FileObject</code>
	 */
	public FileObject getAttachment(int index) {
		FileObject fo = null;
		if (attachments != null) {
			fo = attachments.get(index);
		}
		return fo;
	}

	/**
	 * addAttachment
	 * @param fileObject
	 */
	public void addAttachment(FileObject fileObject) {
		if (attachments == null) {
			attachments = new ArrayList<FileObject>();
		}
		attachments.add(fileObject);
	}
}
